package com.shugl.study.leetcode;

/**
 * Description 两个 非空 的链表，表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储
 * 将两个数相加，并以相同形式返回一个表示和的链表
 *
 * @author Shugl
 * @version 1.0
 * @since 2021/6/10 10:28
 */
public class ListNodeSum {

    public static void main(String[] args) {
        ListNode l1 = new ListNode(9);
        ListNode next = l1.next;
        for (int i = 0; i < 6; i++) {
            next = next == null ? l1 : next;
            next.next = new ListNode(9);
            next = next.next;
        }
//        while (l1 != null) {
//            System.out.println(l1.val);
//            l1 = l1.next;
//        }
        ListNode l2 = new ListNode(9);
        ListNode next2 = l2.next;
        for (int i = 0; i < 3; i++) {
            next2 = next2 == null ? l2 : next2;
            next2.next = new ListNode(9);
            next2 = next2.next;
        }
//        while (l2 != null) {
//            System.out.println(l2.val);
//            l2 = l2.next;
//        }
        ListNode listNode = new ListNodeSum().addTwoNumbers(l1, l2);
        while (listNode != null) {
            System.out.println(listNode.val);
            listNode = listNode.next;
        }
    }

    /**
     * 功能描述: 反向进位的计算方式
     *
     * @return com.shugl.study.leetcode.ListNode
     * @author shugl
     * @date 2021/6/15 18:08
     */
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode reNode = new ListNode();
        ListNode next = reNode.next;
        int up = 0;
        for (; ; ) {
            next = next == null ? reNode : next;
            int sum = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + up;
            next.val = sum % 10;
            up = sum / 10;
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }

            if (l1 == null && l2 == null && up == 0) {
                return reNode;
            }
            next.next = new ListNode();
            next = next.next;
        }

    }

    /**
     * 功能描述: 另一种实现思路，未完成
     *
     * @param
     * @return com.shugl.study.leetcode.ListNode
     * @author shugl
     * @date 2021/6/15 18:07
     */
    public ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
        int l1Num = 0;
        int sc = 1;
        while (l1 != null) {
            l1Num += l1.val * sc;
            l1 = l1.next;
            sc = sc * 10;
        }
        l1Num = l1Num / 10;
        int l2Num = 0;
        sc = 1;
        while (l2 != null) {
            l2Num += l2.val * sc;
            l2 = l2.next;
            sc = sc * 10;
        }
        l2Num = l2Num / 10;
        int sum = l1Num + l2Num;
        String sumStr = String.valueOf(sum);
        ListNode reNode = new ListNode();
        ListNode next = reNode.next;
        for (int i = sumStr.length() - 1; i > 0; i--) {
            next = next == null ? reNode : next;
            next.val = Integer.parseInt(sumStr.charAt(i) + "");
            next.next = new ListNode();
            next = next.next;
        }
        return reNode;
    }
}

class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}
